package com.joesem.sale.client.logic.workOrder.viewModel

import android.app.Application
import com.joesem.sale.client.base.BaseViewModel
import com.joesem.sale.client.base.request
import com.joesem.sale.client.base.toast
import com.joesem.sale.client.network.data.*


class WorkOrderViewModel(application: Application) : BaseViewModel(application) {
    var orderDetails: OrderDetailsMode? = null
    fun workOrderParams() {
        request(request = {
            workOrderParams()
        }, responseFailed = {
            toast(it.message)
            actionIntent(ParamsDataIntent(null))
        }, response = {
            if (it.data != null) {
                actionIntent(ParamsDataIntent(it.data))
            } else {
                actionIntent(ParamsDataIntent(null))
            }
        })
    }

    fun orderStatsNum() {
        request(request = {
            orderStatsNum()
        }, response = {
            it.data?.let {
                actionIntent(OrderCuntIntent(it))
            }
        }, responseFailed = {

        })
    }

    fun noticeCunt() {
        request(request = {
            noticeCount()
        }, response = {
            it.data?.let {
                actionIntent(NoticeCuntIntent(it))
            }
        }, responseFailed = {

        })
    }

    fun getOrderList(
        date_type: String,
        date_start: String,
        date_end: String,
        about_me: String,
        status: String,
        type: String = "",
        isFirst: Boolean
    ) {
        request(request = {
            workOrder(
                date_type = date_type,
                date_start = date_start,
                date_end = date_end,
                about_me = about_me,
                status = status,
                type = type,
                isFirst = isFirst
            )
        }, response = {
            actionIntent(ListDataIntent(it.data?.filterNotNull() ?: arrayListOf(), isFirst))
        }, responseFailed = {
            toast(it.message)
            actionIntent(ListDataIntent(arrayListOf(), isFirst))
        })
    }

    fun getOrderDetails(id: String, isLoading: Boolean = false) {
        request(isLoading = isLoading, request = {
            workOrderDetails(id)
        }, response = { it ->
            orderDetails = it.data
            it.data?.let {
                actionIntent(OrderDetailsIntent(it))
            } ?: toast("暂无无数据\n${it.msg}")
        }, responseFailed = {
            toast(it.message + "\n下拉可以尝试刷新~")
        })
    }

    fun orderAccept() {
        request(isLoading = true, request = {
            orderAccept(orderDetails?.id)
        }, response = {
            orderDetails = it.data
            toast(it.msg)
            it.data?.let {
                actionIntent(OrderDetailsIntent(it))
            } ?: toast("暂无无数据\n${it.msg}")
        }, responseFailed = {
            toast(it.message)
        })
    }

    fun orderStart() {
        request(isLoading = true, request = {
            orderStart(orderDetails?.id)
        }, response = {
            orderDetails = it.data
            toast(it.msg)
            it.data?.let {
                actionIntent(OrderDetailsIntent(it))
            } ?: toast("暂无无数据\n${it.msg}")
        }, responseFailed = {
            toast(it.message)
        })
    }


    class ListDataIntent(val data: List<WorkOrderMode>, val isFirst: Boolean)

    class ParamsDataIntent(val data: WorkOrderParamsMode?)

    class OrderDetailsIntent(val data: OrderDetailsMode)

    class OrderCuntIntent(val data: OrderStatsNumMode)

    class NoticeCuntIntent(val data: NoticeCountMode)
}